package com.samebuilding.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;




public class OnlineUserList extends TimerTask {

	ServletContext application;
	
	public OnlineUserList(ServletContext application){
		this.application = application;
	}
	public void run() {
		// TODO Auto-generated method stub
		List onlineUser = new ArrayList();
		List firstFifty;
		if(application.getAttribute("firstFifty")==null){
			firstFifty = new ArrayList();
		}else{
			firstFifty = (List)application.getAttribute("firstFifty");
		}
		if(application.getAttribute("onlineUser")!=null){
			onlineUser = (List)application.getAttribute("onlineUser");
		}
		Collections.sort(onlineUser);
		
		if(firstFifty.size()==0){
			for(int i = 0; i < 50; i++){
				if(i < onlineUser.size()){
					firstFifty.add(onlineUser.get(i));
				}
			}
			application.setAttribute("firstFifty", firstFifty);
			application.setAttribute("showList", firstFifty);
		}else{
			List secondFifty = new ArrayList();
			for(int i = 0; i < 50; i++){
				if(i < onlineUser.size()&&!firstFifty.contains(onlineUser.get(i))){
					secondFifty.add(onlineUser.get(i));
				}
			}
			firstFifty.clear();
			application.setAttribute("firstFifty", firstFifty);
			application.setAttribute("showList", secondFifty);
		}
	}
}
